<template>
	<view class="main">
		<view class="title">
			<text v-if="appPF == 'mp-wx'" @click="copyAll()">简易模式</text>
		</view>
		<view class="lucky">
			<view class="picker-bar" v-if="!pIsShow" @click="pickerShow">点此使用幸运号码</view>
			<view class="picker" v-else>
				<text class="btn" @click="pickerShow">点此清空幸运号码</text>
				<view class="spool" >
					<b class="red" v-for="item in pikerSpool.red" :key="item.no" @click="pickerNo('red',item.no)" :class="item.ok?'active':''">
						{{formatAdd0(item.no)}}
					</b>
				</view>
				<view class="spool">
					<b class="blue" v-for="item in pikerSpool.blue" :key="item.no" @click="pickerNo('blue',item.no)" :class="item.ok?'active':''">
						{{formatAdd0(item.no)}}
					</b>
				</view>
			</view>
			<view class="dog" v-if="pIsShow">
				<!-- <view class="btn row b1" :class="group==1?'active':''" @click="yyyShow(1)">
					<text class="db-t">来一组</text>
				</view>
				<view class="btn row b2" :class="group==3?'active':''" @click="yyyShow(3)">
					<text class="db-t">来三组</text>
				</view>
				<view class="btn row b3" :class="group==5?'active':''" @click="yyyShow(5)">
					<text class="db-t">来五组</text>
				</view> -->
				<view class="btn single" @click="yyyShow(1)">
					<text class="db-t">选完开整</text>
				</view>
			</view>
			<view class="dog" v-else style="flex-direction: column;top: 45px;">
				<view class="btn b1" :class="group==1?'active':''" @click="yyyShow(1)">
					<text class="db-t">独占鳌头</text>
					<text class="db-p">生成一组号码</text>
				</view>
				<view class="btn b2" :class="group==3?'active':''" @click="yyyShow(3)">
					<text class="db-t">连中三元</text>
					<text class="db-p">生成三组号码</text>
				</view>
				<view class="btn b3" :class="group==5?'active':''" @click="yyyShow(5)">
					<text class="db-t">五福临门</text>
					<text class="db-p">生成五组号码</text>
				</view>
				<view style="width: 100%;text-align: center;padding: 10px;color: #999;">
					{{'<< '}}左滑查看历史开奖记录	
				</view>
			</view>
		</view>
		<uni-popup ref="POP_UP" mask-background-color="rgba(0,0,0,0)">
			<view class="yyy" @touchstart="touchS" @touchmove.stop.prevent="touchM" @touchend="touchE">
				<view class="result" v-if="yyyAllow">
					<view class="tip">
						<text>摇一摇生成号码</text>
						<image class="img" src="../../static/imgs/y.gif"></image>
					</view>
					<view class="list" v-if="!isNull(resList)">
						<view class="item" v-for="item in resList" :key="item">
							<block v-if="isNull(item.term)">
								<view class="item-c" v-for="(item,index) in item" :key="item">
									<text class="ball" :class="index == 6?'blue':'red'">{{formatAdd0(item)}}</text>
								</view>
							</block>
							<block v-else>
								<view style="flex-direction: column;">
									<view class="item-c">
										<text>您选择的号码曾在{{item.date}}第{{item.term}}期命中大奖</text>
									</view>
									<view class="item-c">
										<text class="ball red">{{formatAdd0(item.ball1)}}</text>
										<text class="ball red">{{formatAdd0(item.ball2)}}</text>
										<text class="ball red">{{formatAdd0(item.ball3)}}</text>
										<text class="ball red">{{formatAdd0(item.ball4)}}</text>
										<text class="ball red">{{formatAdd0(item.ball5)}}</text>
										<text class="ball red">{{formatAdd0(item.ball6)}}</text>
										<text class="ball blue">{{formatAdd0(item.ball7)}}</text>
									</view>
								</view>
							</block>
						</view>
						<view class="btn-share" @click="shareResult">
							<uni-icons :type="appType=='android'?'weixin':'compose'" size="28" color="#fff"></uni-icons>
							<text class="text">{{appType=='android'?'号码发送到微信':'复制号码'}}</text>
						</view>
					</view>
					<view style="width: 100%;text-align: center;padding: 10px;color: #999;">
						任意位置下滑关闭此界面	
					</view>
				</view>
				<view class="result img" v-else>
					<image class="gif" src="../../static/imgs/yyy.gif"></image>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import request from "@/util/request.js"
	import util from "@/util/util.js"
	import { onHide } from "@dcloudio/uni-app";
	import {
		ref,
		onMounted,
		reactive
	} from "vue"
	export default {
		setup() {
			onMounted(() => {
				yyyEnd()
			})
			
			onHide(()=>{
				console.log('hide')
				yyyEnd()
			})
			
			let appType = ref('android')
			// #ifdef MP-WEIXIN
			appType = ref('weixin')
			// #endif

			let resList = ref([])
			let group = ref(0)
			async function yyyGo() {
				let _red = pikerSpool.red
				let _blue = pikerSpool.blue
				let _luckyNo = []
				for(let i in _red) {
					if (_red[i].ok) _luckyNo.push(_red[i].no)
				}
				while (_luckyNo.length < 6) {
					_luckyNo.push(0)
				}
				let blueBall = 0
				for(let i in _blue) {
					if (_blue[i].ok) blueBall = _blue[i].no
				}
				_luckyNo.push(blueBall)
				let p = {}
				p.group = group.value==0?1:group.value
				p.luckyNo = _luckyNo
				console.log(p)
				let data = []
				let res = await request.goSsq(p)
				console.log(res)
				if (res.code == 10000) {
					data = util.isNull(res.data)?{term: '幸运号码好像不太幸运，换个试一试'}:res.data
					resList.value = data
					_resStr = ''
					if (util.isNull(data[0].term)) {
						for(let i in data) {
							_resStr = _resStr+'\n'+JSON.stringify(data[i])
						}
					} else {
						_resStr = data[0].date+'第'+data[0].term+'期:['+data[0].ball1+','+data[0].ball2+','+data[0].ball3+','+data[0].ball4+','+data[0].ball5+','+data[0].ball6+','+data[0].ball7+']'
					}
				}
			}
			// share result
			let _resStr = ''
			function shareResult() {
				if (appType.value == 'weixin') {
					uni.setClipboardData({
						data: _resStr,
						success: (res) => {
							uni.showToast({
								title: '号码复制成功'
							})
						}
					})
				} else {
					uni.share({
						provider: "weixin",
							scene: "WXSceneSession",
							type: 1,
							summary: _resStr,
							success: function (res) {
								console.log("success:" + JSON.stringify(res));
							},
							fail: function (err) {
								console.log("fail:" + JSON.stringify(err));
							}
					});
				}
			}
			
			// choose number
			let pikerSpool = reactive({
				red: [],
				blue: [],
			})
			
			let pIsShow = ref(false)
			function pickerShow() {
				pIsShow.value = !pIsShow.value
				
				if (pIsShow.value) {
					let _red = [],_blue = []
					for (let i=1;i<=33;i++){
						_red.push({no: i, ok: false})
						if (i<=15) _blue.push({no: i, ok: false})
					}
					pikerSpool.red = _red
					pikerSpool.blue = _blue
				}
			}
			
			function pickerNo(f,ino) {
				if (f == 'red') {
					let _red = pikerSpool.red
					let redOkCount = 0
					for (let i in _red) {
						if (_red[i].ok) redOkCount++
					}
					for (let i in _red) {
						if (_red[i].no == ino) {
							if (_red[i].ok) {
								_red[i].ok = false
							} else if (!_red[i].ok) {
								if (redOkCount<6) {
									_red[i].ok = true
								} else {
									uni.showToast({
										title: '红球最多选6个',
										icon: 'none',
										duration: 1500,
									})
								}
							}
						}
					}
				}
				if (f == 'blue') {
					let _blue = pikerSpool.blue
					for (let i in _blue) {
						if (_blue[i].no == ino) {
							_blue[i].ok = !_blue[i].ok
						} else {
							_blue[i].ok = false
						}
					}
				}
			}
			
			// yyy
			let POP_UP = ref(null)
			function yyyShow(g) {
				if (g != group.value) resList.value = []
				if (g > 0) {
					group.value = g
					yyyStart()
					POP_UP.value.open('bottom')
				} else {
					POP_UP.value.close()
					yyyEnd()
				}
			}
			
			let yyyAllow = ref(true)
			let yyyDuration = 2500
			let isStart = true
			let startTime = 0
			let aceX = 0,aceY = 0
			function yyyStart() {
				// resList.value = []
				let speedMin = 3500
				// #ifdef MP-WEIXIN
				speedMin = 900
				// #endif
				uni.onAccelerometerChange((res) => {
					// console.log(`x:${res.x},y:${res.y}z:${res.z}`)
					if (isStart) {
						isStart = false
						startTime = (new Date()).getTime()
						aceX = res.x
						aceY = res.y
					} else {
						let endTime = (new Date()).getTime()
						let speedX = (res.x - aceX) / (endTime - startTime) * 100000
						let speedY = (res.y - aceY) / (endTime - startTime) * 100000
						startTime = endTime
						aceX = res.x
						aceY = res.y
						// console.log(Math.abs(speedX),Math.abs(speedY))
						// if ((Math.abs(speedX) > speedMin) || (Math.abs(speedY) > speedMin)) {
						if ((Math.abs(speedX) > speedMin)) {
								console.log(`x:${res.x},y:${res.y}z:${res.z}`)
								yyyAction()
						}
					}
				})
				uni.startAccelerometer({
					interval: "normal",
					success(res) {
						console.log('startAccelerometer success')
					},
					fail(err) {
						console.log('startAccelerometer fail',err)
					}
				})
			}
			function yyyEnd() {
				uni.stopAccelerometer({
					success(res) {
						console.log('stopAccelerometer success!')
					},
					fail(err) {
						console.log('stopAccelerometer fail!',err)
					}
				})
			}
			function yyyAction() {
				if (yyyAllow.value) {
					yyyAllow.value = false
					yyyGo()
					yyyPlay()
					setTimeout(()=>{
						yyyAllow.value = true
					},yyyDuration)
				}
			}
			function yyyPlay() {
				// #ifdef APP-PLUS 
				let path = plus.io.convertLocalFileSystemURL("_www/static/mp3/coin.mp3");  
				if (plus.os.name == 'Android') {  
				    let MediaPlayer = plus.android.importClass("android.media.MediaPlayer");  
				    let audioPlayer = new MediaPlayer()  
				    audioPlayer.setDataSource(path); //指定音频文件路径  
				    audioPlayer.setLooping(true); //设置为循环播放  
				    audioPlayer.prepare(); //初始化播放器MediaPlayer  
				    audioPlayer.start();  
				    // console.log(audioPlayer.getDuration());  // 获取音乐的总时长  
						setTimeout(()=>{
							audioPlayer.stop()
							plus.device.vibrate(500)
						},yyyDuration)
				}  
				// #endif
				// #ifdef MP-WEIXIN
				uni.setInnerAudioOption({
					obeyMuteSwitch: false, //（仅在 iOS 生效）是否遵循静音开关，设置为 false 之后，即使是在静音模式下，也能播放声音
					fail: function(e) {
						console.log('e', e)
					}
				});
				const innerAudioContext = uni.createInnerAudioContext();
				innerAudioContext.autoplay = true;
				innerAudioContext.src = "/static/mp3/coin.mp3";
				innerAudioContext.onPlay((res) => {
					console.log('onPlayRes:',res)
				});
				innerAudioContext.onError((err) => {
					console.log('onErrorRes:',err)
				});
				setTimeout(() => {
					innerAudioContext.destroy() // 音效一般为1s，2s后销毁实例
					uni.vibrateLong({
						success(res) {
							console.log(res)
						},
						fail(err) {
							console.log(err)
						}
					})
				}, yyyDuration)
				// #endif
			}
			
			// 下滑关闭，上划yyygo
			let startRange = 0
			let endRange = 0
			function touchS(e) {
				// console.log('start',e.touches[0])
				startRange = e.touches[0].clientY
			}
			function touchM(e) {
				// console.log('move',e.touches[0].clientY,e.touches[0].pageY,e.touches[0].force)
				endRange = e.touches[0].clientY
			}
			function touchE(e) {
				// console.log('end',e)
				if (endRange - startRange > 80) {
					yyyShow(0)
					startRange = 0
					endRange = 0
				} else if (startRange - endRange > 80) {
					// yyyAction()
					startRange = 0
					endRange = 0
				}
			}
			
			// render util
			function isNull(s) {
				return util.isNull(s)
			}
			
			function formatAdd0(s) {
				return s*1<10?'0'+s:s
			}
			
			let appPF = ref(util.getPlayForm())
			function copyAll() {
				uni.setClipboardData({
					data: '重启小程序即可进入简易模式',
					success: (res) => {
						uni.setStorageSync('not-simple',false)
						uni.showToast({
							title: '重启小程序即可进入简易模式',
							icon: 'none'
						})
					}
				})
			}

			return {
				appType,resList,group,shareResult,
				pikerSpool,pIsShow,pickerShow,pickerNo,
				POP_UP,yyyShow,yyyGo,yyyStart,yyyEnd,yyyAllow,
				touchS,touchM,touchE,isNull,formatAdd0,
				appPF,copyAll
			}
		},
	}
</script>
<style lang="scss" scoped src="./com_page.scss"></style>
